package controllers;

import beans.LocalizedBean;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import models.DigitalClassSession;

public class AttendDigitalClassController extends HttpServlet {
    private Dbmanager dbManager;
    private QueryManager queryManager;
    private DigitalClassSession digitalClassSession;
    
    /*
     * HTTP GET Request
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sessie = request.getSession();
        
        if(request.getParameter("ID") == null){
            DigitalClassSession oldClass = (DigitalClassSession) sessie.getAttribute("digitalClassSession");
            dbManager = new Dbmanager();
            dbManager.openConnection();
            queryManager = new QueryManager(dbManager); 
            if(oldClass != null){
                digitalClassSession = queryManager.getDigitalClassSession(oldClass.getDigitalClassId());
                dbManager.closeConnection();
                if(digitalClassSession != null){
                    sessie.setAttribute("digitalClassSession", digitalClassSession);               
                }
            }
        } else {
            dbManager = new Dbmanager();
            dbManager.openConnection();
            queryManager = new QueryManager(dbManager); 
            digitalClassSession = queryManager.getDigitalClassSession(Integer.parseInt(request.getParameter("ID")));
            dbManager.closeConnection();
            sessie.setAttribute("digitalClassSession", digitalClassSession);            
        }
        
       
        if(digitalClassSession != null){
            request.setAttribute("digitalClassSession", digitalClassSession);
            
            String address = "/attend_digital_class.jsp";
            RequestDispatcher dispatcher = request.getRequestDispatcher(address);
            dispatcher.forward(request, response);
        }
    }
}